
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>3.11 DOM获取 & DOM操作 · gitbook电子书</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-toggle-chapters/toggle.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="键盘事件&鼠标事件.html" />
    
    
    <link rel="prev" href="定时器&防抖&节流.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    前言
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1.1" data-path="../使用/使用.html">
            
                <a href="../使用/使用.html">
            
                    
                    md语法 & gitbook使用
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../面试题/面试题.html">
            
                <a href="../面试题/面试题.html">
            
                    
                    面试题
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../面试题/html.html">
            
                <a href="../面试题/html.html">
            
                    
                    HTML
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../面试题/css.html">
            
                <a href="../面试题/css.html">
            
                    
                    CSS
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../面试题/js.html">
            
                <a href="../面试题/js.html">
            
                    
                    JavaScript
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../面试题/vue.html">
            
                <a href="../面试题/vue.html">
            
                    
                    Vue
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.5" data-path="../面试题/小程序.html">
            
                <a href="../面试题/小程序.html">
            
                    
                    小程序
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.6" data-path="../面试题/编程题.html">
            
                <a href="../面试题/编程题.html">
            
                    
                    编程题
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../html/">
            
                <a href="../html/">
            
                    
                    1. HTML
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../html/1.常用标签.html">
            
                <a href="../html/1.常用标签.html">
            
                    
                    1.1 常用标签
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../html/2.列表.html">
            
                <a href="../html/2.列表.html">
            
                    
                    1.2 列表
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../html/3.表单.html">
            
                <a href="../html/3.表单.html">
            
                    
                    1.3 表单属性
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="../html/4.音频&视频.html">
            
                <a href="../html/4.音频&视频.html">
            
                    
                    1.4 ifame & audio & video
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../css/">
            
                <a href="../css/">
            
                    
                    2. CSS
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../css/1.选择器.html">
            
                <a href="../css/1.选择器.html">
            
                    
                    2.1 选择器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../css/2.字体样式.html">
            
                <a href="../css/2.字体样式.html">
            
                    
                    2.2 字体样式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../css/3.背景.html">
            
                <a href="../css/3.背景.html">
            
                    
                    2.3 背景
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="../css/4.盒子模型.html">
            
                <a href="../css/4.盒子模型.html">
            
                    
                    2.4 盒子模型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="../css/5.浮动.html">
            
                <a href="../css/5.浮动.html">
            
                    
                    2.5 浮动
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="../css/6.定位和渐变.html">
            
                <a href="../css/6.定位和渐变.html">
            
                    
                    2.6 定位 & 渐变
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7" data-path="../css/7.过渡&变形.html">
            
                <a href="../css/7.过渡&变形.html">
            
                    
                    2.7 过渡 & 变形
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.8" data-path="../css/8.动画.html">
            
                <a href="../css/8.动画.html">
            
                    
                    2.8 动画
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.9" data-path="../css/9.滤镜.html">
            
                <a href="../css/9.滤镜.html">
            
                    
                    2.9 滤镜
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.10" data-path="../css/10.布局.html">
            
                <a href="../css/10.布局.html">
            
                    
                    2.10 布局
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="./">
            
                <a href="./">
            
                    
                    3. JS
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="数据类型.html">
            
                <a href="数据类型.html">
            
                    
                    3.1 数据类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="语句.html">
            
                <a href="语句.html">
            
                    
                    操作符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="操作符.html">
            
                <a href="操作符.html">
            
                    
                    语句
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="函数.html">
            
                <a href="函数.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.5" data-path="基本类型和引用类型的值.html">
            
                <a href="基本类型和引用类型的值.html">
            
                    
                    基本类型和引用类型的值
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.6" data-path="执行环境及作用域.html">
            
                <a href="执行环境及作用域.html">
            
                    
                    执行环境及作用域
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.7" data-path="垃圾收集.html">
            
                <a href="垃圾收集.html">
            
                    
                    垃圾收集
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.8" data-path="Object类型.html">
            
                <a href="Object类型.html">
            
                    
                    Object类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.9" data-path="Array类型.html">
            
                <a href="Array类型.html">
            
                    
                    Array类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.10" data-path="Date类型.html">
            
                <a href="Date类型.html">
            
                    
                    Date类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.11" data-path="string&array.html">
            
                <a href="string&array.html">
            
                    
                    3.2 String & Array
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.12" data-path="date.html">
            
                <a href="date.html">
            
                    
                    3.3 Date类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.13" data-path="正则表达式.html">
            
                <a href="正则表达式.html">
            
                    
                    3.4 RegExp类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.14" data-path="global&math.html">
            
                <a href="global&math.html">
            
                    
                    3.5 Global对象 & Math对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.15" data-path="变量&作用域&垃圾收集.html">
            
                <a href="变量&作用域&垃圾收集.html">
            
                    
                    3.6 变量 & 作用域 & 垃圾收集
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.16" data-path="流程控制&函数.html">
            
                <a href="流程控制&函数.html">
            
                    
                    3.7 流程控制 & 函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.17" data-path="面向对象.html">
            
                <a href="面向对象.html">
            
                    
                    3.8 面向对象的程序设计
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.18" data-path="递归&冒泡&快速.html">
            
                <a href="递归&冒泡&快速.html">
            
                    
                    3.9 递归 & 冒泡 & 快速
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.19" data-path="定时器&防抖&节流.html">
            
                <a href="定时器&防抖&节流.html">
            
                    
                    3.10 定时器\防抖\节流
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.5.20" data-path="DOM获取&DOM操作.html">
            
                <a href="DOM获取&DOM操作.html">
            
                    
                    3.11 DOM获取 & DOM操作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.21" data-path="键盘事件&鼠标事件.html">
            
                <a href="键盘事件&鼠标事件.html">
            
                    
                    3.12 键盘事件 & 鼠标事件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.22" data-path="事件冒泡&事件代理&事件绑定.html">
            
                <a href="事件冒泡&事件代理&事件绑定.html">
            
                    
                    3.13 事件冒泡\代理\绑定
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.23" data-path="this指向.html">
            
                <a href="this指向.html">
            
                    
                    3.14 this指向
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.24" data-path="Ajax&跨域.html">
            
                <a href="Ajax&跨域.html">
            
                    
                    3.15 Ajax & 跨域
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.25" data-path="Bom.html">
            
                <a href="Bom.html">
            
                    
                    3.16 BOM
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.26" data-path="ES6.html">
            
                <a href="ES6.html">
            
                    
                    3.17 ES6
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.27" data-path="js实例.html">
            
                <a href="js实例.html">
            
                    
                    3.18 js实例
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../vue/">
            
                <a href="../vue/">
            
                    
                    4. Vue
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="../vue/1.基础.html">
            
                <a href="../vue/1.基础.html">
            
                    
                    4.1 基础
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="../vue/2.生命周期.html">
            
                <a href="../vue/2.生命周期.html">
            
                    
                    4.2 生命周期
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3" data-path="../vue/3.vuex.html">
            
                <a href="../vue/3.vuex.html">
            
                    
                    4.3 vuex
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.4" data-path="../vue/4.原理.html">
            
                <a href="../vue/4.原理.html">
            
                    
                    4.4 深入响应式原理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.5" data-path="../vue/5.组件.html">
            
                <a href="../vue/5.组件.html">
            
                    
                    4.5 组件进阶
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.6" data-path="../vue/6.规模化.html">
            
                <a href="../vue/6.规模化.html">
            
                    
                    4.6 规模化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.7" data-path="../vue/7.过渡&动画.html">
            
                <a href="../vue/7.过渡&动画.html">
            
                    
                    4.7 过渡 & 动画
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.8" data-path="../vue/8.可复用性&组合.html">
            
                <a href="../vue/8.可复用性&组合.html">
            
                    
                    4.8 可复用性 & 组合
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.9" data-path="../vue/9.工具.html">
            
                <a href="../vue/9.工具.html">
            
                    
                    4.9 工具
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../Node/">
            
                <a href="../Node/">
            
                    
                    5. Node
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../插件/">
            
                <a href="../插件/">
            
                    
                    插件
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >3.11 DOM获取 & DOM操作</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="dom&#x83B7;&#x53D6;">DOM&#x83B7;&#x53D6;</h1>
<p>DOM:Document Object Model(&#x6587;&#x6863;&#x5BF9;&#x8C61;&#x6A21;&#x578B;)        </p>
<blockquote>
<p>getElementBy    </p>
</blockquote>
<pre><code class="lang-js">    <span class="hljs-comment">//&#x901A;&#x8FC7;id&#x83B7;&#x53D6;</span>
    <span class="hljs-keyword">var</span> test1 = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">&quot;text1&quot;</span>);
    <span class="hljs-built_in">console</span>.log(test1);
    <span class="hljs-comment">//&#x901A;&#x8FC7;class&#x540D;&#x83B7;&#x53D6;  &#x8FD4;&#x56DE;&#x7684;&#x662F;&#x4E00;&#x4E2A;&#x7C7B;&#x6570;&#x7EC4;&#xFF08;&#x957F;&#x7684;&#x60F3;&#x6570;&#x7EC4;&#xFF0C;&#x6CA1;&#x6709;&#x6570;&#x7EC4;&#x7684;&#x65B9;&#x6CD5;&#xFF09;</span>
    <span class="hljs-keyword">var</span> item = <span class="hljs-built_in">document</span>.getElementsByClassName(<span class="hljs-string">&quot;item&quot;</span>);
    <span class="hljs-built_in">console</span>.log(item[<span class="hljs-number">0</span>]);
    <span class="hljs-comment">//&#x901A;&#x8FC7;&#x6807;&#x7B7E;&#x540D;&#x83B7;&#x53D6;</span>
    <span class="hljs-keyword">var</span> li = test1.getElementsByTagName(<span class="hljs-string">&quot;li&quot;</span>);
    <span class="hljs-built_in">console</span>.log(li);
    <span class="hljs-comment">//&#x901A;&#x8FC7;name&#x5C5E;&#x6027;&#x83B7;&#x53D6;</span>
    <span class="hljs-keyword">var</span> username = <span class="hljs-built_in">document</span>.getElementsByName(<span class="hljs-string">&quot;username&quot;</span>)
    <span class="hljs-built_in">console</span>.log(username);
</code></pre>
<blockquote>
<p>querySelector    </p>
</blockquote>
<pre><code class="lang-js">    <span class="hljs-comment">//&#x53EF;&#x4EE5;&#x63A5;&#x53D7;css3&#x9009;&#x62E9;&#x5668;</span>
    <span class="hljs-keyword">var</span> ol = <span class="hljs-built_in">document</span>.querySelector(<span class="hljs-string">&quot;.olClass li&quot;</span>)
    <span class="hljs-built_in">console</span>.log(ol)
    <span class="hljs-keyword">var</span> li = <span class="hljs-built_in">document</span>.querySelectorAll(<span class="hljs-string">&quot;.olClass li&quot;</span>)
    <span class="hljs-built_in">console</span>.log(li)
</code></pre>
<blockquote>
<p>getElement &#x548C; querySelector &#x5DEE;&#x5F02;<br>getElement&#x52A8;&#x6001;&#x83B7;&#x53D6;<br>querySelector&#x9759;&#x6001;&#x83B7;&#x53D6;    </p>
</blockquote>
<pre><code class="lang-js">    <span class="hljs-comment">//&#x83B7;&#x53D6;ol&#x4E0B;&#x7684;li</span>
    <span class="hljs-keyword">var</span> ol = <span class="hljs-built_in">document</span>.getElementsByTagName(<span class="hljs-string">&quot;ol&quot;</span>)[<span class="hljs-number">0</span>];
    <span class="hljs-keyword">var</span> ol_getElement = ol.getElementsByTagName(<span class="hljs-string">&quot;li&quot;</span>);
    <span class="hljs-comment">//&#x83B7;&#x53D6;ol&#x4E0B;&#x7684;li</span>
    <span class="hljs-keyword">var</span> ol_querySlector = <span class="hljs-built_in">document</span>.querySelectorAll(<span class="hljs-string">&quot;ol&gt;li&quot;</span>);

    <span class="hljs-comment">//&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x8282;&#x70B9;</span>
    <span class="hljs-keyword">var</span> newLi = <span class="hljs-built_in">document</span>.createElement(<span class="hljs-string">&quot;li&quot;</span>);
    <span class="hljs-comment">//&#x5728;ol&#x672B;&#x5C3E;&#x6DFB;&#x52A0;&#x4E00;&#x4E2A;li</span>
    ol.appendChild(newLi);  

    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;getElement&quot;</span>,ol_getElement)
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;querySelector&quot;</span>,ol_querySlector)

    <span class="hljs-comment">//HTMLCollection &#x662F;&#x4E2A;&#x52A8;&#x6001;&#x7684;&#x5217;&#x8868;</span>
    <span class="hljs-comment">//NodeList &#x9759;&#x6001;&#x7684;&#x96C6;&#x5408;</span>
</code></pre>
<h1 id="dom&#x64CD;&#x4F5C;">DOM&#x64CD;&#x4F5C;</h1>
<h3 id="dom&#x83B7;&#x53D6;">DOM&#x83B7;&#x53D6;</h3>
<pre><code class="lang-js">    <span class="hljs-comment">//getElementsById</span>
    <span class="hljs-comment">//getElementsByClassName</span>
    <span class="hljs-comment">//getElementsbyName</span>
    <span class="hljs-keyword">var</span> ul = <span class="hljs-built_in">document</span>.getElementsByTagName(<span class="hljs-string">&quot;ul&quot;</span>)[<span class="hljs-number">0</span>];
    <span class="hljs-comment">//&#x7C7B;&#x6570;&#x7EC4;.length&#x5C5E;&#x6027;&#x53CD;&#x6620;&#x51FA;&#x67E5;&#x8BE2;&#x9875;&#x9762;&#x7ED3;&#x6784;&#x7684;&#x4E2A;&#x6570;</span>
    <span class="hljs-comment">//console.log(ul[0],ul.length);</span>

    <span class="hljs-comment">//querySelector    &#x652F;&#x6301;css3&#x9009;&#x62E9;&#x5668;</span>
    <span class="hljs-comment">//querySelectorAll &#x83B7;&#x53D6;&#x9759;&#x6001;&#x7684;NodeLIist&#x5217;&#x8868;</span>

    <span class="hljs-comment">//&#x83B7;&#x53D6;&#x5B50;&#x8282;&#x70B9;</span>
    <span class="hljs-comment">//childNodes &#x5305;&#x542B; &#x6587;&#x672C;&#x8282;&#x70B9;&#x548C;&#x5143;&#x7D20;&#x8282;&#x70B9;&#xFF08;&#x6807;&#x7B7E;&#xFF09;</span>
    <span class="hljs-comment">//childElementCount &#x5B50;&#x5143;&#x7D20;&#x8282;&#x70B9;&#x7684;&#x4E2A;&#x6570;</span>
    <span class="hljs-comment">//children &#x6240;&#x6709;&#x7684;&#x5B50;&#x5143;&#x7D20;&#x8282;&#x70B9;</span>
    <span class="hljs-built_in">console</span>.log(ul.childNodes,ul.childElementCount,ul.children);
    <span class="hljs-comment">//&#x7B2C;&#x4E00;&#x4E2A;&#x5B50;&#x5143;&#x7D20; &#x548C; &#x6700;&#x540E;&#x4E00;&#x4E2A;&#x5B50;&#x5143;&#x7D20;</span>
    <span class="hljs-comment">//&#x5E26;Element&#x5B57;&#x773C;&#x7684;&#x8FD4;&#x56DE;&#x5143;&#x7D20;&#x8282;&#x70B9;</span>
    <span class="hljs-built_in">console</span>.log(ul.firstChild,ul.firstElementChild);
    <span class="hljs-built_in">console</span>.log(ul.lastChild,ul.lastElementChild);

    <span class="hljs-comment">//&#x4E0A;&#x4E00;&#x4E2A;&#x8282;&#x70B9;&#x548C;&#x4E0B;&#x4E00;&#x4E2A;&#x8282;&#x70B9;</span>
    <span class="hljs-comment">//&#x5982;&#x679C;&#x5230;&#x4E86;&#x5C3D;&#x5934;&#xFF0C;&#x8868;&#x793A;&#x6CA1;&#x6709;&#x4E0B;&#x4E00;&#x4E2A;&#x6216;&#x4E0A;&#x4E00;&#x4E2A;&#xFF0C;&#x8FD4;&#x56DE;null</span>
    <span class="hljs-keyword">var</span> li = <span class="hljs-built_in">document</span>.getElementsByTagName(<span class="hljs-string">&quot;li&quot;</span>);
    <span class="hljs-built_in">console</span>.log(li[<span class="hljs-number">2</span>].nextSibling,li[<span class="hljs-number">2</span>].nextElementSibling);
    <span class="hljs-built_in">console</span>.log(li[<span class="hljs-number">2</span>].previousSibling,li[<span class="hljs-number">2</span>].previousElementSibling);

    <span class="hljs-comment">//&#x7236;&#x8282;&#x70B9;</span>
    <span class="hljs-built_in">console</span>.log(li[<span class="hljs-number">2</span>].parentNode,li[<span class="hljs-number">2</span>].parentElement)
</code></pre>
<h3 id="dom&#x5220;&#x9664;">DOM&#x5220;&#x9664;</h3>
<pre><code class="lang-js">    <span class="hljs-keyword">var</span> ul = <span class="hljs-built_in">document</span>.getElementsByTagName(<span class="hljs-string">&quot;ul&quot;</span>)[<span class="hljs-number">0</span>];
        <span class="hljs-comment">//&#x5220;&#x9664;&#x5B50;&#x8282;&#x70B9;</span>
    <span class="hljs-comment">//            ul.removeChild(ul.firstElementChild);</span>
        <span class="hljs-comment">//&#x5220;&#x9664;&#x81EA;&#x8EAB;</span>
    <span class="hljs-comment">//            ul.remove();</span>

    <span class="hljs-comment">//DOM &#x4E2D;&#x7684;onclick&#x5C5E;&#x6027;&#x8868;&#x793A;&#x70B9;&#x51FB;&#x65F6;&#x6267;&#x884C;&#x64CD;&#x4F5C;</span>
    <span class="hljs-comment">//DOM &#x4E2D;&#x7684;this&#x6307;&#x7684;&#x662F;&#x89E6;&#x53D1;&#x4E8B;&#x4EF6;&#x7684;&#x5BF9;&#x8C61;&#xFF08;&#x5143;&#x7D20;&#xFF09;</span>
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">del</span>(<span class="hljs-params">el</span>)</span>{
        <span class="hljs-built_in">console</span>.log(el)
        el.parentElement.remove();
    }

    <span class="hljs-keyword">var</span> input = <span class="hljs-built_in">document</span>.getElementsByName(<span class="hljs-string">&quot;username&quot;</span>)[<span class="hljs-number">0</span>];
    <span class="hljs-keyword">var</span> input_val = input.value;
    <span class="hljs-built_in">console</span>.log(input.value);
</code></pre>
<h3 id="dom&#x6DFB;&#x52A0;">DOM&#x6DFB;&#x52A0;</h3>
<pre><code class="lang-js">    <span class="hljs-keyword">var</span> ul = <span class="hljs-built_in">document</span>.getElementsByTagName(<span class="hljs-string">&quot;ul&quot;</span>)[<span class="hljs-number">0</span>];
    <span class="hljs-comment">//&#x521B;&#x5EFA;li</span>
    <span class="hljs-keyword">var</span> newLi = <span class="hljs-built_in">document</span>.createElement(<span class="hljs-string">&quot;li&quot;</span>)
    <span class="hljs-comment">//&#x961F;&#x5C3E;&#x63D2;&#x5165;</span>
    ul.appendChild(newLi);
    <span class="hljs-comment">//&#x6307;&#x5B9A;&#x4F4D;&#x7F6E;&#x63D2;&#x5165;  insertBefore(&#x65B0;&#x8282;&#x70B9;&#xFF0C;&#x63D2;&#x5165;&#x5230;&#x67D0;&#x4E2A;&#x5143;&#x7D20;&#x4E4B;&#x524D;)</span>
    <span class="hljs-comment">//&#x7B2C;&#x4E8C;&#x4E2A;&#x53C2;&#x6570;&#x4E3A;null&#xFF0C;&#x5219;&#x63D2;&#x5165;&#x961F;&#x5C3E;</span>
    <span class="hljs-comment">//&#x82E5;&#x63D2;&#x5165;&#x5143;&#x7D20;&#x5DF2;&#x5B58;&#x5728;&#x7236;&#x8282;&#x70B9;&#x4E2D;&#xFF0C;insertBefore&#x4F1A;&#x66F4;&#x65B0;&#x8FD9;&#x4E2A;&#x5143;&#x7D20;&#x7684;&#x4F4D;&#x7F6E;</span>
    ul.insertBefore(newLi,ul.children[<span class="hljs-number">2</span>])

    <span class="hljs-comment">//&#x514B;&#x9686;&#x8282;&#x70B9;</span>
    <span class="hljs-comment">//cloneNode&#xFF08;&#x5E03;&#x5C14;&#x503C;&#xFF09;  &#x9ED8;&#x8BA4;false&#xFF0C;&#x6D45;&#x62F7;&#x8D1D;&#xFF1A;&#xFF1B; true&#x4E3A;&#x6DF1;&#x62F7;&#x8D1D;&#xFF0C;&#x8FDE;&#x540C;&#x5B50;&#x5143;&#x7D20;&#x7684;&#x7ED3;&#x6784;&#x90FD;&#x8FDB;&#x884C;&#x514B;&#x9686;</span>
    <span class="hljs-keyword">var</span> newUl = ul.cloneNode(<span class="hljs-literal">true</span>);
    <span class="hljs-built_in">document</span>.body.appendChild(newUl);

    <span class="hljs-comment">//&#x6DFB;&#x52A0;&#x5185;&#x5BB9;</span>
    <span class="hljs-comment">//innerText &#x53EA;&#x66F4;&#x6539;&#x5143;&#x7D20;&#x91CC;&#x7684;&#x6587;&#x672C;</span>
    <span class="hljs-comment">//            newLi.innerText = &quot;&#x8FD9;&#x662F;&#x65B0;&#x52A0;&#x7684;li&#x6807;&#x7B7E;&quot; ;</span>

    <span class="hljs-comment">//innerHTML &#x5982;&#x679C;&#x5B57;&#x7B26;&#x4E32;&#x5185;&#x5305;&#x542B;HTML&#x4EE3;&#x7801;&#xFF0C;&#x4F1A;&#x5C06;&#x4EE3;&#x7801;&#x90E8;&#x5206;&#x89E3;&#x6790;&#x5E76;&#x8F93;&#x51FA;HTML&#x7ED3;&#x6784;</span>
    newLi.innerHTML = <span class="hljs-string">&quot;&lt;h1&gt;&#x8FD9;&#x662F;&#x65B0;&#x52A0;&#x7684;li&#x6807;&#x7B7E;&lt;/h1&gt;&quot;</span> ;
    <span class="hljs-comment">//outerHTML &#x548C; innerHTML&#x5BF9;&#x6BD4;&#xFF0C;&#x4F5C;&#x7528;&#x5728;&#x5143;&#x7D20;&#x7684;&#x672C;&#x8EAB;</span>
    newLi.outerHTML = <span class="hljs-string">&quot;&lt;h1&gt;text&lt;/h1&gt;&quot;</span>;
</code></pre>
<h3 id="dom&#x6837;&#x5F0F;">DOM&#x6837;&#x5F0F;</h3>
<pre><code class="lang-javascript">    <span class="hljs-keyword">var</span> div = <span class="hljs-built_in">document</span>.getElementsByClassName(<span class="hljs-string">&quot;box&quot;</span>)[<span class="hljs-number">0</span>];
    <span class="hljs-comment">//&#x8BBE;&#x7F6E;&#x8282;&#x70B9;&#x7684;&#x5C5E;&#x6027;</span>
    div.id = <span class="hljs-string">&quot;123&quot;</span>
    <span class="hljs-built_in">console</span>.log(div.className);  <span class="hljs-comment">//box</span>
    <span class="hljs-built_in">console</span>.log(div.nodeName)    <span class="hljs-comment">//DIV</span>
    <span class="hljs-comment">//&#x83B7;&#x53D6;</span>
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;&#x83B7;&#x53D6;&#x5C5E;&#x6027;&#xFF1A;&quot;</span>,div.getAttribute(<span class="hljs-string">&quot;data-type&quot;</span>));
    <span class="hljs-comment">//&#x8BBE;&#x7F6E;&#x5C5E;&#x6027;</span>
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;&#x8BBE;&#x7F6E;&#x5C5E;&#x6027;&#xFF1A;&quot;</span>,div.setAttribute(<span class="hljs-string">&quot;style&quot;</span>,<span class="hljs-string">&quot;color: red;&quot;</span>))
    <span class="hljs-comment">//&#x5220;&#x9664;&#x5C5E;&#x6027; &#x8FDE;&#x5C5E;&#x6027;&#x540D;&#x4E5F;&#x53BB;&#x9664;</span>
    div.removeAttribute(<span class="hljs-string">&apos;style&apos;</span>);

    <span class="hljs-comment">//&#x7C7B;&#x540D;</span>
    div.className += <span class="hljs-string">&quot; active&quot;</span>;
    <span class="hljs-comment">//classlist&#x8868;&#x793A;&#x5143;&#x7D20;&#x5305;&#x542B;&#x7684;class&#x7684;&#x5217;&#x8868;&#xFF0C;value&#x8868;&#x793A;&#x539F;&#x672C;class&#x7684;&#x503C;</span>
    <span class="hljs-built_in">console</span>.log(div.classList) ;
    <span class="hljs-comment">//&#x6DFB;&#x52A0;&#x7C7B;&#x540D;</span>
    div.classList.add(<span class="hljs-string">&quot;fz30&quot;</span>);
    <span class="hljs-comment">//&#x79FB;&#x9664;&#x7C7B;&#x540D;</span>
    div.classList.remove(<span class="hljs-string">&quot;active&quot;</span>);

    <span class="hljs-built_in">console</span>.log(div.className)
    <span class="hljs-comment">//&#x901A;&#x8FC7;&#x70B9;&#x51FB;button&#xFF0C;&#x63A7;&#x5236;div&#x6DFB;&#x52A0;&#x6216;&#x5220;&#x9664;&#x67D0;&#x4E2A;&#x7C7B;&#x540D;&#xFF0C;&#x8FDB;&#x884C;&#x5207;&#x6362;</span>
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">showDiv</span>(<span class="hljs-params"></span>)</span>{
    <span class="hljs-comment">//                if (div.className.indexOf(&quot;active&quot;)==-1){</span>
    <span class="hljs-comment">//                    //&#x4E0D;&#x5B58;&#x5728;&#x5C31;&#x662F;&#x8981;&#x6DFB;&#x52A0;</span>
    <span class="hljs-comment">//                    div.classList.add(&quot;active&quot;);</span>
    <span class="hljs-comment">//                }else{</span>
    <span class="hljs-comment">//                    //&#x5B58;&#x5728;&#x5C31;&#x8981;&#x53BB;&#x79FB;&#x9664;</span>
    <span class="hljs-comment">//                    div.classList.remove(&quot;active&quot;);</span>
    <span class="hljs-comment">//                }</span>
        <span class="hljs-comment">//&#x5207;&#x6362;&#x7C7B;&#x540D;</span>
        div.classList.toggle(<span class="hljs-string">&quot;active&quot;</span>);
    }

    <span class="hljs-comment">//&#x8BBE;&#x7F6E;&#x884C;&#x5185;&#x6837;&#x5F0F;</span>
    div.style.fontSize = <span class="hljs-string">&quot;30px&quot;</span>;
    div.style.color = <span class="hljs-string">&quot;red&quot;</span>;
    <span class="hljs-comment">//&#xFF01;important</span>
    <span class="hljs-comment">//console.log(div.style.color)</span>
    <span class="hljs-comment">//&#x6700;&#x7EC8;&#x6E32;&#x67D3;&#x6837;&#x5F0F;</span>
    <span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">window</span>.getComputedStyle(div) );

    <span class="hljs-comment">//console.log(div.currentStyle) </span>
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getStyle</span>(<span class="hljs-params"> element ,style</span>)</span>{
        <span class="hljs-keyword">if</span>(<span class="hljs-built_in">window</span>.getComputedStyle){
            <span class="hljs-comment">//&#x652F;&#x6301;getComputedStyle</span>
            <span class="hljs-keyword">if</span>(style==<span class="hljs-string">&quot;float&quot;</span>){
                style = <span class="hljs-string">&quot;cssFloat&quot;</span>;
            }
            <span class="hljs-keyword">return</span> <span class="hljs-built_in">window</span>.getComputedStyle(element)[style];
        }<span class="hljs-keyword">else</span>{
            <span class="hljs-comment">//IE7&#x4EE5;&#x4E0B;&#x7684;&#x65B9;&#x6CD5;</span>
            <span class="hljs-keyword">if</span>(style==<span class="hljs-string">&quot;float&quot;</span>){
                style = <span class="hljs-string">&quot;styleFloat&quot;</span>;
            }
            <span class="hljs-keyword">return</span> element.currentStyle[style];
        }
    }
    <span class="hljs-comment">//&#x6700;&#x7EC8;&#x6837;&#x5F0F; &#x6D6E;&#x52A8;&#x7684;&#x5C5E;&#x6027;&#x540D;&#x5728;&#x4E0D;&#x540C;&#x6D4F;&#x89C8;&#x5668;&#x4F1A;&#x4E0D;&#x4E00;&#x81F4;</span>
    <span class="hljs-comment">//&#x53D8;&#x5F62;transform&#x7684;&#x503C;&#x662F;&#x4EE5;&#x77E9;&#x9635;&#x7684;&#x65B9;&#x5F0F;&#x663E;&#x793A;</span>
    <span class="hljs-built_in">console</span>.log(getStyle(div,<span class="hljs-string">&quot;float&quot;</span>));
</code></pre>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="定时器&防抖&节流.html" class="navigation navigation-prev " aria-label="Previous page: 3.10 定时器\防抖\节流">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="键盘事件&鼠标事件.html" class="navigation navigation-next " aria-label="Next page: 3.12 键盘事件 & 鼠标事件">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"3.11 DOM获取 & DOM操作","level":"1.5.20","depth":2,"next":{"title":"3.12 键盘事件 & 鼠标事件","level":"1.5.21","depth":2,"path":"JS/键盘事件&鼠标事件.md","ref":"JS/键盘事件&鼠标事件.md","articles":[]},"previous":{"title":"3.10 定时器\\防抖\\节流","level":"1.5.19","depth":2,"path":"JS/定时器&防抖&节流.md","ref":"JS/定时器&防抖&节流.md","articles":[]},"dir":"ltr"},"config":{"plugins":["toggle-chapters","livereload"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"toggle-chapters":{},"livereload":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"gitbook电子书","gitbook":"*","description":"记录前端相关的知识点以及一些示例的demo"},"file":{"path":"JS/DOM获取&DOM操作.md","mtime":"2019-03-21T07:44:16.701Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-09-06T08:37:43.293Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-toggle-chapters/toggle.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

